var db = require('../../config/db')
//更改用户信息
exports.up = (req, res)=>{
    if(req.body.tar == '1'){
        var sqlStr =  'select * from user_info where name=? and id=?'
        db.query(sqlStr, [req.body.name, req.body.id], (err, results)=>{ 
            if(err){
                return res.cc('500', err)
            }
            if(results.length ==1){
                const sql= 'update user_info  set phone=?, address=?, sex=?, age=? WHERE id=?'
                db.query(sql, [req.body.phone, req.body.address, req.body.sex, req.body.age, req.body.id], (err, results1)=>{
                    if(err) return res.cc('4004',err)
                    if(results1.affectedRows !== 1) return res.cc('4004','修改基本信息失败')
                    res.send({ //返回前端信息
                        status: 200,
                        message: '信息修改成功',
                    })
                })
            }
            else{
                db.query(`select * from user_info where name=?`, req.body.name,(err, results2) => {
                    if(results2.length ==1){
                        res.send({ // 返回前端信息
                            status: 500,
                            message: '修改失败，存在重名',
                        });
                    }else{
                        const sq= 'update user_info  set name=?,phone=?, address=?, sex=?, age=? WHERE id=?'
                        db.query(sq, [req.body.name,req.body.phone, req.body.address, req.body.sex, req.body.age, req.body.id], (err, results3)=>{
                            if(err) return res.cc('40',err)
                            if(results3.affectedRows !== 1) return res.cc('400','修改基本信息失败')
                            res.send({ //返回前端信息
                                status: 200,
                                message: '信息修改成功',
                            })
                        })
                    }
                })
            }
        })

    }else{
        var sqlStr =  'select * from admin_info where name=? and id=?'
        db.query(sqlStr, [req.body.name, req.body.id], (err, results)=>{ 
        if(err){
            return res.cc('500', err)
        }
        if(results.length ==1){
            const sql= 'update admin_info  set phone=?, address=?, sex=?, age=? WHERE id=?'
            db.query(sql, [req.body.phone, req.body.address, req.body.sex, req.body.age, req.body.id], (err, results1)=>{
                if(err) return res.cc('4004',err)
                if(results1.affectedRows !== 1) return res.cc('4004','修改基本信息失败')
                res.send({ //返回前端信息
                    status: 200,
                    message: '信息修改成功',
                })
            })
        }
        else{
            db.query(`select * from admin_info where name=?`, req.body.name,(err, results2) => {
                if(results2.length ==1){
                    res.send({ // 返回前端信息
                        status: 500,
                        message: '修改失败，存在重名',
                    });
                }else{
                    const sq= 'update admin_info  set name=?,phone=?, address=?, sex=?, age=? WHERE id=?'
                    db.query(sq, [req.body.name,req.body.phone, req.body.address, req.body.sex, req.body.age, req.body.id], (err, results3)=>{
                        if(err) return res.cc('40',err)
                        if(results3.affectedRows !== 1) return res.cc('400','修改基本信息失败')
                        res.send({ //返回前端信息
                            status: 200,
                            message: '信息修改成功',
                        })
                    })
                }
            })
        }
        })

    }
}

exports.upavatar = (req, res)=>{
    if(req.body.tar == '1'){
        db.query('update user_info  set avatar=? WHERE id=?', [req.body.avatar, req.body.id], (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('500', '上传失败');
            res.send({ //返回前端信息
                status: 200,
                message: '头像修改成功',
            })
        })
    }else{
        db.query('update admin_info  set avatar=? WHERE id=?', [req.body.avatar, req.body.id], (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('500', '上传失败');
            res.send({ //返回前端信息
                status: 200,
                message: '头像修改成功',
            })
        })

    }
    
}
exports.uppassword = (req, res)=>{
    if(req.body.tar == '1'){
        db.query('update user_info  set password=? WHERE id=?', [req.body.password, req.body.id], (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('500', '修该失败');
            res.send({ //返回前端信息
                status: 200,
                message: '密码修改成功',
            })
        })
    }else{
        db.query('update admin_info  set password=? WHERE id=?', [req.body.password, req.body.id], (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('500', '修该失败');
            res.send({ //返回前端信息
                status: 200,
                message: '密码修改成功',
            })
        })
        
    }
    
}